﻿using System;
using System.Runtime.InteropServices;
using NppPlugin.DllExport;

namespace NppPluginNET
{
	internal class UnmanagedExports
	{
		[DllExport(CallingConvention = CallingConvention.Cdecl)]
		private static bool isUnicode()
		{
			return true;
		}

		[DllExport(CallingConvention = CallingConvention.Cdecl)]
		private static void setInfo(NppData notepadPlusData)
		{
			VisualStudioLineCopy.nppData = notepadPlusData;
			VisualStudioLineCopy.CommandMenuInit();
		}

		[DllExport(CallingConvention = CallingConvention.Cdecl)]
		private static IntPtr getFuncsArray(ref int nbF)
		{
			nbF = VisualStudioLineCopy._funcItems.Items.Count;
			return VisualStudioLineCopy._funcItems.NativePointer;
		}

		[DllExport(CallingConvention = CallingConvention.Cdecl)]
		private static uint messageProc(uint Message, IntPtr wParam, IntPtr lParam)
		{
			return 1;
		}

		private static IntPtr _ptrPluginName = IntPtr.Zero;

		[DllExport(CallingConvention = CallingConvention.Cdecl)]
		private static IntPtr getName()
		{
			if (_ptrPluginName == IntPtr.Zero)
				_ptrPluginName = Marshal.StringToHGlobalUni(VisualStudioLineCopy.PluginName);
			return _ptrPluginName;
		}

		[DllExport(CallingConvention = CallingConvention.Cdecl)]
		private static void beNotified(IntPtr notifyCode)
		{
			SCNotification nc = (SCNotification)Marshal.PtrToStructure(notifyCode, typeof (SCNotification));
			if (nc.nmhdr.code == (uint)NppMsg.NPPN_SHUTDOWN)
			{
				VisualStudioLineCopy.PluginCleanUp();
				Marshal.FreeHGlobal(_ptrPluginName);
			}
		}
	}
}